import { EnumWorkMode, Industry, JobType, MemberIntentionResult, MemberIntentionStep1Request, Region } from "@/apis/endpoint/model"
import { pageMapper } from "@/constants/pageMap"
import { navigate } from "@/utils/navigation"
import { useImmer } from "use-immer"
import { validateScheme } from "@/utils/app"
import { postMyIntentionstep1 } from "@/apis/endpoint/my"
import Taro from "@tarojs/taro"
import { isSetValue } from "@/utils/helper"

interface StepOneState {
  workMode?: EnumWorkMode
  jobTypeId?: number
  jobTypeName?: string
  industryIds?: number[]
  industryNames?: string[]
  cityId?: string
  cityName?: string
}

export function useStepOne(next: IFn) {
  const [stepOneState, setStepOneState] = useImmer<StepOneState>({
    workMode: EnumWorkMode.全职
  })
  function onWorkModeChange(value: EnumWorkMode) {
    setStepOneState(draft => {draft.workMode = value})
  }
  function handleEditJobType() {
    const query = stepOneState.jobTypeId ? {jobtypeid: stepOneState.jobTypeId} : null
    navigate(pageMapper.select.jobtype, query, "navigateTo", {
      events: {
        chooseJobType(value: JobType) {
          setStepOneState(draft => {
            draft.jobTypeId = value.id
            draft.jobTypeName = value.name
          })
        }
      }
    })
  }
  function handleEditIndustry() {
    const query = stepOneState.industryIds?.length ? {industryids: stepOneState.industryIds.join(",")} : null
    navigate(pageMapper.select.industry, query, "navigateTo", {
      events: {
        chooseIndustry(values: Industry[]) {
          setStepOneState(draft => {
            draft.industryIds = values.map(l =>l.id)
            draft.industryNames = values.map(l =>l.name)
          })
        }
      }
    })
  }
  function handleEditCity() {
    const query = stepOneState.cityId ? {cityid: stepOneState.cityId} : null
    navigate(pageMapper.select.city, query, "navigateTo", {
      events: {
        chooseCity(value: Region) {
          setStepOneState(draft => {
            draft.cityId = value.id
            draft.cityName = value.name
          })
        }
      }
    })
  }
  function receiveStepOne(setting: MemberIntentionResult) {
    const patchFields: (keyof StepOneState)[] = ["workMode", "jobTypeId", "jobTypeName", "industryIds", "industryNames", "cityId", "cityName"]
    let patch: INullable<{[key in keyof StepOneState]: MemberIntentionResult[key]}> = null
    patchFields.forEach(field => {
      if (isSetValue(setting[field])) {
        patch ??= {}
        // @ts-ignore
        patch[field] = setting[field]
      }
    })
    if (patch) {
      setStepOneState(draft => {
        Object.keys(patch!)
        .forEach(field => {
          draft[field] = patch![field]
        })
      })
    }
  }
  async function saveStepOne() {
    const params = {} as MemberIntentionStep1Request
    const valid = validateScheme(stepOneState, params, {
      workMode: "workMode",
      jobTypeId: "jobTypeId",
      industryIds: "industryIds",
      cityId: "cityId"
    }, {
      workMode: {
        field: "workMode",
        dataField: "workMode",
        message: "请选择职位类型"
      },
      jobTypeId: {
        field: "jobTypeId",
        dataField: "jobTypeId",
        message: "请选择期望职位"
      },
      industryIds: {
        field: "industryIds",
        dataField: "industryIds",
        message: "请选择期望行业"
      },
      cityId: {
        field: "cityId",
        dataField: "cityId",
        message: "请选择工作城市"
      }
    })
    if (!valid) return
    const nextId = await postMyIntentionstep1(params).send()
    Taro.showToast({
      icon: "success",
      title: "设置成功"
    })
    next(nextId)
  }
  return {
    stepOneState,
    onWorkModeChange,
    handleEditJobType,
    handleEditIndustry,
    handleEditCity,
    saveStepOne,
    receiveStepOne
  }
}
